<template>
  <div>
    <el-card>
      <template #header>
        <h2>修改账号</h2>
      </template>
      <div>
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="70px"
          class="demo-ruleForm"
          size="mini"
        >
          <el-form-item label="原密码" prop="oldPsw">
            <el-input
              type="password"
              v-model="ruleForm.oldPsw"
              autocomplete="off"
              @blur="checkOldPsw"
            ></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newPsw">
            <el-input
              type="password"
              v-model="ruleForm.newPsw"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPsw">
            <el-input
              type="password"
              v-model="ruleForm.checkPsw"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >确定</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { checkPass, confirmPsw, checkNew } from '../../utils/validate.js'
import { checkPswOld, checkPswNew } from '../../api/users.js'
import local from '../../utils/local'
export default {
  data() {
    return {
      ruleForm: {
        oldPsw: '',
        newPsw: '',
        checkPsw: ''
      },
      rules: {
        oldPsw: [{ validator: checkPass, trigger: 'blur' }],
        newPsw: [{ validator: checkNew(this), trigger: 'blur' }],
        checkPsw: [{ validator: confirmPsw(this, 'newPsw'), trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // 修改密码
          const data = await checkPswNew({
            newPwd: this.ruleForm.newPsw
          })
          if (data.code === 0) {
            this.$message({
              message: data.msg,
              type: 'success'
            })
          }
          setTimeout(() => {
            local.remove('token')
            location.reload()
          }, 1500)
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 验证原密码
    async checkOldPsw(e) {
      // console.log(e.target.value)
      if (e.target.value.trim().length !== 0) {
        const data = await checkPswOld({
          oldPwd: e.target.value
        })
        // console.log(data)
        if (data.code === '00') {
          this.$message({
            message: '原密码正确',
            type: 'success'
          })
        } else {
          this.$message.error('原密码输入错误')
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-input {
  width: 240px;
}
</style>
